package net.towee.model.account.impl;

import java.util.ArrayList;
import java.util.List;

import net.towee.model.account.AccountIdentity;
import net.towee.model.account.ContactsGroup;
import net.towee.model.group.GroupIdentity;
import net.towee.server.persistence.impl.MongoDbStore;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.PostLoad;
import com.google.code.morphia.annotations.Reference;

/**
 * Group of contacts implementation
 * @author c58
 */
@Entity(MongoDbStore.ACCOUNT_CONTACTS_COLLECTION)
public class ContactsGroupImpl implements ContactsGroup {
	@Id private String id;
	private String name;
	@Reference private List<AccountIdentity> users;
	
	public ContactsGroupImpl(){}
	public ContactsGroupImpl(String id, String name){
		this(id);
		setName(name);
	}
	public ContactsGroupImpl(String id){
		this.id = id;
		this.users = new ArrayList<AccountIdentity>();
	}
	
	@PostLoad
	private void createEmptyArrays(){
		if(users == null)
			users = new ArrayList<AccountIdentity>();
	}
	
	@Override
	public ContactsGroup toContactsGroup() {
		return this;
	}

	@Override
	public GroupIdentity toUsersGroup() {
		return null;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void addContact(AccountIdentity account) {
		this.users.add(account);
	}

	@Override
	public void deleteContact(AccountIdentity account) {
		this.users.remove(account);
	}

	@Override
	public boolean hasContact(AccountIdentity account) {
		return this.users.contains(account);
	}

	@Override
	public void clearGroup() {
		this.users.clear();
	}
	@Override
	public List<AccountIdentity> getContactsList() {
		return users;
	}
	
	@Override
	public boolean equals(Object o){
		if(o == this)
			return true;
		else if(o instanceof ContactsGroup && this.getId().equals( ((ContactsGroup)o).getId() ))
			return true;
		
		return false;
	}
}
